import { defineStore } from 'pinia';
import axios from 'axios';
import { computed, ref } from 'vue';
// shop模块
export const useShopStore = defineStore('shop', () => {

  const lists = ref([]);
  const status = {
    1: '未完成',
    2: '进行中',
    3: '已完成',
  };

  const request = axios.create({
    baseURL: 'http://127.0.0.1:3009',
  });

  const getLists = async function (where = {}) {
    const { data } = await request.get('/lists', {
      params: where,
    });
    lists.value = data;
  };

  // 搜索

  const searchByStatus = async function (status) {
    if (status === 0) {
      await getLists()
    } else {
      await getLists({ status })
    }
  }

  // 更新

  const updateStatusList = async function (id, status) {
    await request.patch(`/lists/${id}`, { status });
    getLists();
  }

  // 删除

  const delList = async function (id) {
    await request.delete(`/lists/${id}`);
    getLists();
  }

  // 添加

  const addList = async function (data) {
    await request.post('/lists', data);
    getLists();
  };

  return {
    lists,
    status,
    getLists,
    addList,
    delList,
    updateStatusList,
    searchByStatus,
  }
});

